<?php
if (isset($_SESSION['player_id'])) {
$query = "SELECT `role_id`, `team_id`
				FROM `players`
				WHERE `id`='{$_SESSION['player_id']}'
				LIMIT 1";
	$sql = mysql_query($query) or die(mysql_error());
	if (mysql_num_rows($sql)==1)
	{
	   $cap=mysql_fetch_assoc($sql);
       if ($cap['role_id']==1 && $cap['team_id']!=''){
	       //echo 'Вы можете редактировать данные своей команды!';



           if (isset($_GET['action']) && $_GET['action']=='delete' &&
                isset($_GET['game_id']) && is_numeric($_GET['game_id'])){
                $query = "DELETE FROM `application`
				WHERE `game_id`={$_GET['game_id']} AND `team_id`={$cap['team_id']}";
                $sql = mysql_query($query) or die(mysql_error());
                }





            if (isset($_GET['action']) && $_GET['action']=='applicate' &&
           isset($_GET['game_id']) && is_numeric($_GET['game_id'])){
                $query = "SELECT `game_id`
				FROM `application`
				WHERE `game_id`={$_GET['game_id']} AND `team_id`={$cap['team_id']} LIMIT 1";

                $sql = mysql_query($query) or die(mysql_error());
                if (!mysql_num_rows($sql)){
                $query = "INSERT INTO `application`
                   SET
                   `game_id`={$_GET['game_id']},
                   `team_id`={$cap['team_id']},
                   `status`=0";
                mysql_query($query) or die(mysql_error());
           }

           }
           ?>

          <table border="0" cellpadding="0" cellspacing="0" width="100%">
            <tr bgcolor="#000000">
            <td width="100%">
             <table border="0" width="100%">
    	<tr bgcolor="#222222">
	<td valign="bottom" colspan="2" >
	Название
	</td>
	<td valign="bottom" colspan="2" >
	Начало
	</td>

	<td valign="bottom" colspan="2" >
	Заявка
	</td>
	</tr>

<?php

$query = "SELECT `id`, `title`, `status`, DATE_FORMAT(start_date, '%d.%m.%Y %H:%i')as start_date
				FROM `game`
				WHERE `status`=1 OR `status`=2";
$sql = mysql_query($query) or die(mysql_error());
    for ($i=1;$i<=mysql_num_rows($sql);$i++){
        $sqlgame=mysql_fetch_assoc($sql);

		$query = "SELECT `status`, `pin`
				FROM `application`
				WHERE `game_id`={$sqlgame['id']} AND `team_id`={$cap['team_id']} LIMIT 1";

		$sqlstatus = mysql_query($query) or die(mysql_error());
        $sqlgamestatus=mysql_fetch_assoc($sqlstatus);


        print "<tr bgcolor=\"#333333\">
        <td valign=\"bottom\" colspan=\"2\">{$sqlgame['title']}</td>
        <td valign=\"bottom\" colspan=\"2\">{$sqlgame['start_date']}</td>
        <td valign=\"bottom\" colspan=\"2\">";
        if ($sqlgame['status']==1){
        if (!isset($sqlgamestatus['status']))
        print "<a href=\"index.php?section=team&mode=application&action=applicate&game_id={$sqlgame['id']}\">Подать</a>";
        }
        else
        {
        if (!isset($sqlgamestatus['status'])){        	 print "Набор завершён";
        	 }}

        if (isset($sqlgamestatus['status']) && $sqlgamestatus['status']==0)
        print "Рассматривается | <a href=\"index.php?section=team&mode=application&action=delete&game_id={$sqlgame['id']}\">[Отозвать]</a>";


        if ($sqlgamestatus['status']==1){
        print "Принята (pin: <b>{$sqlgamestatus['pin']}</b>)";
        if ($sqlgame['status']!=2){
        print "<a href=\"index.php?section=team&mode=application&action=delete&game_id={$sqlgame['id']}\"> [Отозвать]</a>";
        }                                }

        if ($sqlgamestatus['status']==2)
        print "Отказано";
		print "</td></tr>";

    }


?>






    	</table>
	</td>
	</tr>
	</table>
    <?

}
}
}
else print ('Время вашей сессии истекло!');

?>